package pe.gob.mpfn.controller;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.el.ELContext;
import javax.el.ExpressionFactory;
import javax.faces.application.Application;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.component.html.HtmlOutputText;
import javax.faces.component.html.HtmlPanelGrid;
import javax.faces.context.FacesContext;
import javax.faces.validator.LengthValidator;
import org.primefaces.component.autocomplete.AutoComplete;
import org.primefaces.component.inputtext.InputText;
import org.primefaces.component.inputtextarea.InputTextarea;
import org.primefaces.component.message.Message;
import org.primefaces.context.RequestContext;
import org.primefaces.event.CloseEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.roo.addon.jsf.managedbean.RooJsfManagedBean;
import org.springframework.roo.addon.serializable.RooSerializable;
import pe.gob.mpfn.controller.converter.TablaMaestraConverter;
import pe.gob.mpfn.model.DesarrolloProteccion;
import pe.gob.mpfn.model.Informe;
import pe.gob.mpfn.model.Personal;
import pe.gob.mpfn.model.TablaMaestra;
import pe.gob.mpfn.service.PersonalService;
import pe.gob.mpfn.service.TablaMaestraService;

@Configurable
@ManagedBean(name = "personalBean")
@SessionScoped
@RooSerializable
@RooJsfManagedBean(entity = Personal.class, beanName = "personalBean")
public class PersonalBean implements Serializable  {

	private static final long serialVersionUID = 1L;

	@Autowired
    PersonalService personalService;

	@Autowired
    TablaMaestraService tablaMaestraService;

	private String name = "Personals";

	private Personal personal;

	private List<Personal> allPersonals;

	private boolean dataVisible = false;

	private List<String> columns;

	private HtmlPanelGrid createPanelGrid;

	private HtmlPanelGrid editPanelGrid;

	private HtmlPanelGrid viewPanelGrid;

	private boolean createDialogVisible = false;

	private List<DesarrolloProteccion> selectedDesarrolloProteccions;

	private List<Informe> selectedInformes;

	@PostConstruct
    public void init() {
        columns = new ArrayList<String>();
        columns.add("strNombre");
        columns.add("strApellidoPaterno");
        columns.add("strApellidoMaterno");
        columns.add("strUsuario");
        columns.add("strPassword");
    }

	public String getName() {
        return name;
    }

	public List<String> getColumns() {
        return columns;
    }

	public List<Personal> getAllPersonals() {
        return allPersonals;
    }

	public void setAllPersonals(List<Personal> allPersonals) {
        this.allPersonals = allPersonals;
    }

	public String findAllPersonals() {
        allPersonals = personalService.findAllPersonals();
        dataVisible = !allPersonals.isEmpty();
        return null;
    }

	public boolean isDataVisible() {
        return dataVisible;
    }

	public void setDataVisible(boolean dataVisible) {
        this.dataVisible = dataVisible;
    }

	public HtmlPanelGrid getCreatePanelGrid() {
        if (createPanelGrid == null) {
            createPanelGrid = populateCreatePanel();
        }
        return createPanelGrid;
    }

	public void setCreatePanelGrid(HtmlPanelGrid createPanelGrid) {
        this.createPanelGrid = createPanelGrid;
    }

	public HtmlPanelGrid getEditPanelGrid() {
        if (editPanelGrid == null) {
            editPanelGrid = populateEditPanel();
        }
        return editPanelGrid;
    }

	public void setEditPanelGrid(HtmlPanelGrid editPanelGrid) {
        this.editPanelGrid = editPanelGrid;
    }

	public HtmlPanelGrid getViewPanelGrid() {
        if (viewPanelGrid == null) {
            viewPanelGrid = populateViewPanel();
        }
        return viewPanelGrid;
    }

	public void setViewPanelGrid(HtmlPanelGrid viewPanelGrid) {
        this.viewPanelGrid = viewPanelGrid;
    }

	public HtmlPanelGrid populateCreatePanel() {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        Application application = facesContext.getApplication();
        ExpressionFactory expressionFactory = application.getExpressionFactory();
        ELContext elContext = facesContext.getELContext();
        
        HtmlPanelGrid htmlPanelGrid = (HtmlPanelGrid) application.createComponent(HtmlPanelGrid.COMPONENT_TYPE);
        
        HtmlOutputText desarrolloProteccionsCreateOutput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        desarrolloProteccionsCreateOutput.setId("desarrolloProteccionsCreateOutput");
        desarrolloProteccionsCreateOutput.setValue("Desarrollo Proteccions:   ");
        htmlPanelGrid.getChildren().add(desarrolloProteccionsCreateOutput);
        
        HtmlOutputText desarrolloProteccionsCreateInput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        desarrolloProteccionsCreateInput.setId("desarrolloProteccionsCreateInput");
        desarrolloProteccionsCreateInput.setValue("This relationship is managed from the DesarrolloProteccion side");
        htmlPanelGrid.getChildren().add(desarrolloProteccionsCreateInput);
        
        Message desarrolloProteccionsCreateInputMessage = (Message) application.createComponent(Message.COMPONENT_TYPE);
        desarrolloProteccionsCreateInputMessage.setId("desarrolloProteccionsCreateInputMessage");
        desarrolloProteccionsCreateInputMessage.setFor("desarrolloProteccionsCreateInput");
        desarrolloProteccionsCreateInputMessage.setDisplay("icon");
        htmlPanelGrid.getChildren().add(desarrolloProteccionsCreateInputMessage);
        
        HtmlOutputText informesCreateOutput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        informesCreateOutput.setId("informesCreateOutput");
        informesCreateOutput.setValue("Informes:   ");
        htmlPanelGrid.getChildren().add(informesCreateOutput);
        
        HtmlOutputText informesCreateInput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        informesCreateInput.setId("informesCreateInput");
        informesCreateInput.setValue("This relationship is managed from the Informe side");
        htmlPanelGrid.getChildren().add(informesCreateInput);
        
        Message informesCreateInputMessage = (Message) application.createComponent(Message.COMPONENT_TYPE);
        informesCreateInputMessage.setId("informesCreateInputMessage");
        informesCreateInputMessage.setFor("informesCreateInput");
        informesCreateInputMessage.setDisplay("icon");
        htmlPanelGrid.getChildren().add(informesCreateInputMessage);
        
        HtmlOutputText intCargoIdCreateOutput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        intCargoIdCreateOutput.setId("intCargoIdCreateOutput");
        intCargoIdCreateOutput.setValue("Int Cargo Id:   ");
        htmlPanelGrid.getChildren().add(intCargoIdCreateOutput);
        
        AutoComplete intCargoIdCreateInput = (AutoComplete) application.createComponent(AutoComplete.COMPONENT_TYPE);
        intCargoIdCreateInput.setId("intCargoIdCreateInput");
        intCargoIdCreateInput.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{personalBean.personal.intCargoId}", TablaMaestra.class));
        intCargoIdCreateInput.setCompleteMethod(expressionFactory.createMethodExpression(elContext, "#{personalBean.completeIntCargoId}", List.class, new Class[] { String.class }));
        intCargoIdCreateInput.setDropdown(true);
        intCargoIdCreateInput.setValueExpression("var", expressionFactory.createValueExpression(elContext, "intCargoId", String.class));
        intCargoIdCreateInput.setValueExpression("itemLabel", expressionFactory.createValueExpression(elContext, "#{intCargoId.strDescripcion} #{intCargoId.chrEstado} #{intCargoId.intTablaId}", String.class));
        intCargoIdCreateInput.setValueExpression("itemValue", expressionFactory.createValueExpression(elContext, "#{intCargoId}", TablaMaestra.class));
        intCargoIdCreateInput.setConverter(new TablaMaestraConverter());
        intCargoIdCreateInput.setRequired(false);
        htmlPanelGrid.getChildren().add(intCargoIdCreateInput);
        
        Message intCargoIdCreateInputMessage = (Message) application.createComponent(Message.COMPONENT_TYPE);
        intCargoIdCreateInputMessage.setId("intCargoIdCreateInputMessage");
        intCargoIdCreateInputMessage.setFor("intCargoIdCreateInput");
        intCargoIdCreateInputMessage.setDisplay("icon");
        htmlPanelGrid.getChildren().add(intCargoIdCreateInputMessage);
        
        HtmlOutputText strNombreCreateOutput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        strNombreCreateOutput.setId("strNombreCreateOutput");
        strNombreCreateOutput.setValue("Str Nombre: * ");
        htmlPanelGrid.getChildren().add(strNombreCreateOutput);
        
        InputTextarea strNombreCreateInput = (InputTextarea) application.createComponent(InputTextarea.COMPONENT_TYPE);
        strNombreCreateInput.setId("strNombreCreateInput");
        strNombreCreateInput.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{personalBean.personal.strNombre}", String.class));
        LengthValidator strNombreCreateInputValidator = new LengthValidator();
        strNombreCreateInputValidator.setMaximum(100);
        strNombreCreateInput.addValidator(strNombreCreateInputValidator);
        strNombreCreateInput.setRequired(true);
        htmlPanelGrid.getChildren().add(strNombreCreateInput);
        
        Message strNombreCreateInputMessage = (Message) application.createComponent(Message.COMPONENT_TYPE);
        strNombreCreateInputMessage.setId("strNombreCreateInputMessage");
        strNombreCreateInputMessage.setFor("strNombreCreateInput");
        strNombreCreateInputMessage.setDisplay("icon");
        htmlPanelGrid.getChildren().add(strNombreCreateInputMessage);
        
        HtmlOutputText strApellidoPaternoCreateOutput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        strApellidoPaternoCreateOutput.setId("strApellidoPaternoCreateOutput");
        strApellidoPaternoCreateOutput.setValue("Str Apellido Paterno: * ");
        htmlPanelGrid.getChildren().add(strApellidoPaternoCreateOutput);
        
        InputTextarea strApellidoPaternoCreateInput = (InputTextarea) application.createComponent(InputTextarea.COMPONENT_TYPE);
        strApellidoPaternoCreateInput.setId("strApellidoPaternoCreateInput");
        strApellidoPaternoCreateInput.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{personalBean.personal.strApellidoPaterno}", String.class));
        LengthValidator strApellidoPaternoCreateInputValidator = new LengthValidator();
        strApellidoPaternoCreateInputValidator.setMaximum(50);
        strApellidoPaternoCreateInput.addValidator(strApellidoPaternoCreateInputValidator);
        strApellidoPaternoCreateInput.setRequired(true);
        htmlPanelGrid.getChildren().add(strApellidoPaternoCreateInput);
        
        Message strApellidoPaternoCreateInputMessage = (Message) application.createComponent(Message.COMPONENT_TYPE);
        strApellidoPaternoCreateInputMessage.setId("strApellidoPaternoCreateInputMessage");
        strApellidoPaternoCreateInputMessage.setFor("strApellidoPaternoCreateInput");
        strApellidoPaternoCreateInputMessage.setDisplay("icon");
        htmlPanelGrid.getChildren().add(strApellidoPaternoCreateInputMessage);
        
        HtmlOutputText strApellidoMaternoCreateOutput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        strApellidoMaternoCreateOutput.setId("strApellidoMaternoCreateOutput");
        strApellidoMaternoCreateOutput.setValue("Str Apellido Materno: * ");
        htmlPanelGrid.getChildren().add(strApellidoMaternoCreateOutput);
        
        InputTextarea strApellidoMaternoCreateInput = (InputTextarea) application.createComponent(InputTextarea.COMPONENT_TYPE);
        strApellidoMaternoCreateInput.setId("strApellidoMaternoCreateInput");
        strApellidoMaternoCreateInput.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{personalBean.personal.strApellidoMaterno}", String.class));
        LengthValidator strApellidoMaternoCreateInputValidator = new LengthValidator();
        strApellidoMaternoCreateInputValidator.setMaximum(50);
        strApellidoMaternoCreateInput.addValidator(strApellidoMaternoCreateInputValidator);
        strApellidoMaternoCreateInput.setRequired(true);
        htmlPanelGrid.getChildren().add(strApellidoMaternoCreateInput);
        
        Message strApellidoMaternoCreateInputMessage = (Message) application.createComponent(Message.COMPONENT_TYPE);
        strApellidoMaternoCreateInputMessage.setId("strApellidoMaternoCreateInputMessage");
        strApellidoMaternoCreateInputMessage.setFor("strApellidoMaternoCreateInput");
        strApellidoMaternoCreateInputMessage.setDisplay("icon");
        htmlPanelGrid.getChildren().add(strApellidoMaternoCreateInputMessage);
        
        HtmlOutputText strUsuarioCreateOutput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        strUsuarioCreateOutput.setId("strUsuarioCreateOutput");
        strUsuarioCreateOutput.setValue("Str Usuario: * ");
        htmlPanelGrid.getChildren().add(strUsuarioCreateOutput);
        
        InputTextarea strUsuarioCreateInput = (InputTextarea) application.createComponent(InputTextarea.COMPONENT_TYPE);
        strUsuarioCreateInput.setId("strUsuarioCreateInput");
        strUsuarioCreateInput.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{personalBean.personal.strUsuario}", String.class));
        LengthValidator strUsuarioCreateInputValidator = new LengthValidator();
        strUsuarioCreateInputValidator.setMaximum(45);
        strUsuarioCreateInput.addValidator(strUsuarioCreateInputValidator);
        strUsuarioCreateInput.setRequired(true);
        htmlPanelGrid.getChildren().add(strUsuarioCreateInput);
        
        Message strUsuarioCreateInputMessage = (Message) application.createComponent(Message.COMPONENT_TYPE);
        strUsuarioCreateInputMessage.setId("strUsuarioCreateInputMessage");
        strUsuarioCreateInputMessage.setFor("strUsuarioCreateInput");
        strUsuarioCreateInputMessage.setDisplay("icon");
        htmlPanelGrid.getChildren().add(strUsuarioCreateInputMessage);
        
        HtmlOutputText strPasswordCreateOutput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        strPasswordCreateOutput.setId("strPasswordCreateOutput");
        strPasswordCreateOutput.setValue("Str Password: * ");
        htmlPanelGrid.getChildren().add(strPasswordCreateOutput);
        
        InputTextarea strPasswordCreateInput = (InputTextarea) application.createComponent(InputTextarea.COMPONENT_TYPE);
        strPasswordCreateInput.setId("strPasswordCreateInput");
        strPasswordCreateInput.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{personalBean.personal.strPassword}", String.class));
        LengthValidator strPasswordCreateInputValidator = new LengthValidator();
        strPasswordCreateInputValidator.setMaximum(45);
        strPasswordCreateInput.addValidator(strPasswordCreateInputValidator);
        strPasswordCreateInput.setRequired(true);
        htmlPanelGrid.getChildren().add(strPasswordCreateInput);
        
        Message strPasswordCreateInputMessage = (Message) application.createComponent(Message.COMPONENT_TYPE);
        strPasswordCreateInputMessage.setId("strPasswordCreateInputMessage");
        strPasswordCreateInputMessage.setFor("strPasswordCreateInput");
        strPasswordCreateInputMessage.setDisplay("icon");
        htmlPanelGrid.getChildren().add(strPasswordCreateInputMessage);
        
        HtmlOutputText chrEstadoCreateOutput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        chrEstadoCreateOutput.setId("chrEstadoCreateOutput");
        chrEstadoCreateOutput.setValue("Chr Estado:   ");
        htmlPanelGrid.getChildren().add(chrEstadoCreateOutput);
        
        InputText chrEstadoCreateInput = (InputText) application.createComponent(InputText.COMPONENT_TYPE);
        chrEstadoCreateInput.setId("chrEstadoCreateInput");
        chrEstadoCreateInput.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{personalBean.personal.chrEstado}", Character.class));
        chrEstadoCreateInput.setRequired(false);
        htmlPanelGrid.getChildren().add(chrEstadoCreateInput);
        
        Message chrEstadoCreateInputMessage = (Message) application.createComponent(Message.COMPONENT_TYPE);
        chrEstadoCreateInputMessage.setId("chrEstadoCreateInputMessage");
        chrEstadoCreateInputMessage.setFor("chrEstadoCreateInput");
        chrEstadoCreateInputMessage.setDisplay("icon");
        htmlPanelGrid.getChildren().add(chrEstadoCreateInputMessage);
        
        return htmlPanelGrid;
    }

	public HtmlPanelGrid populateEditPanel() {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        Application application = facesContext.getApplication();
        ExpressionFactory expressionFactory = application.getExpressionFactory();
        ELContext elContext = facesContext.getELContext();
        
        HtmlPanelGrid htmlPanelGrid = (HtmlPanelGrid) application.createComponent(HtmlPanelGrid.COMPONENT_TYPE);
        
        HtmlOutputText desarrolloProteccionsEditOutput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        desarrolloProteccionsEditOutput.setId("desarrolloProteccionsEditOutput");
        desarrolloProteccionsEditOutput.setValue("Desarrollo Proteccions:   ");
        htmlPanelGrid.getChildren().add(desarrolloProteccionsEditOutput);
        
        HtmlOutputText desarrolloProteccionsEditInput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        desarrolloProteccionsEditInput.setId("desarrolloProteccionsEditInput");
        desarrolloProteccionsEditInput.setValue("This relationship is managed from the DesarrolloProteccion side");
        htmlPanelGrid.getChildren().add(desarrolloProteccionsEditInput);
        
        Message desarrolloProteccionsEditInputMessage = (Message) application.createComponent(Message.COMPONENT_TYPE);
        desarrolloProteccionsEditInputMessage.setId("desarrolloProteccionsEditInputMessage");
        desarrolloProteccionsEditInputMessage.setFor("desarrolloProteccionsEditInput");
        desarrolloProteccionsEditInputMessage.setDisplay("icon");
        htmlPanelGrid.getChildren().add(desarrolloProteccionsEditInputMessage);
        
        HtmlOutputText informesEditOutput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        informesEditOutput.setId("informesEditOutput");
        informesEditOutput.setValue("Informes:   ");
        htmlPanelGrid.getChildren().add(informesEditOutput);
        
        HtmlOutputText informesEditInput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        informesEditInput.setId("informesEditInput");
        informesEditInput.setValue("This relationship is managed from the Informe side");
        htmlPanelGrid.getChildren().add(informesEditInput);
        
        Message informesEditInputMessage = (Message) application.createComponent(Message.COMPONENT_TYPE);
        informesEditInputMessage.setId("informesEditInputMessage");
        informesEditInputMessage.setFor("informesEditInput");
        informesEditInputMessage.setDisplay("icon");
        htmlPanelGrid.getChildren().add(informesEditInputMessage);
        
        HtmlOutputText intCargoIdEditOutput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        intCargoIdEditOutput.setId("intCargoIdEditOutput");
        intCargoIdEditOutput.setValue("Int Cargo Id:   ");
        htmlPanelGrid.getChildren().add(intCargoIdEditOutput);
        
        AutoComplete intCargoIdEditInput = (AutoComplete) application.createComponent(AutoComplete.COMPONENT_TYPE);
        intCargoIdEditInput.setId("intCargoIdEditInput");
        intCargoIdEditInput.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{personalBean.personal.intCargoId}", TablaMaestra.class));
        intCargoIdEditInput.setCompleteMethod(expressionFactory.createMethodExpression(elContext, "#{personalBean.completeIntCargoId}", List.class, new Class[] { String.class }));
        intCargoIdEditInput.setDropdown(true);
        intCargoIdEditInput.setValueExpression("var", expressionFactory.createValueExpression(elContext, "intCargoId", String.class));
        intCargoIdEditInput.setValueExpression("itemLabel", expressionFactory.createValueExpression(elContext, "#{intCargoId.strDescripcion} #{intCargoId.chrEstado} #{intCargoId.intTablaId}", String.class));
        intCargoIdEditInput.setValueExpression("itemValue", expressionFactory.createValueExpression(elContext, "#{intCargoId}", TablaMaestra.class));
        intCargoIdEditInput.setConverter(new TablaMaestraConverter());
        intCargoIdEditInput.setRequired(false);
        htmlPanelGrid.getChildren().add(intCargoIdEditInput);
        
        Message intCargoIdEditInputMessage = (Message) application.createComponent(Message.COMPONENT_TYPE);
        intCargoIdEditInputMessage.setId("intCargoIdEditInputMessage");
        intCargoIdEditInputMessage.setFor("intCargoIdEditInput");
        intCargoIdEditInputMessage.setDisplay("icon");
        htmlPanelGrid.getChildren().add(intCargoIdEditInputMessage);
        
        HtmlOutputText strNombreEditOutput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        strNombreEditOutput.setId("strNombreEditOutput");
        strNombreEditOutput.setValue("Str Nombre: * ");
        htmlPanelGrid.getChildren().add(strNombreEditOutput);
        
        InputTextarea strNombreEditInput = (InputTextarea) application.createComponent(InputTextarea.COMPONENT_TYPE);
        strNombreEditInput.setId("strNombreEditInput");
        strNombreEditInput.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{personalBean.personal.strNombre}", String.class));
        LengthValidator strNombreEditInputValidator = new LengthValidator();
        strNombreEditInputValidator.setMaximum(100);
        strNombreEditInput.addValidator(strNombreEditInputValidator);
        strNombreEditInput.setRequired(true);
        htmlPanelGrid.getChildren().add(strNombreEditInput);
        
        Message strNombreEditInputMessage = (Message) application.createComponent(Message.COMPONENT_TYPE);
        strNombreEditInputMessage.setId("strNombreEditInputMessage");
        strNombreEditInputMessage.setFor("strNombreEditInput");
        strNombreEditInputMessage.setDisplay("icon");
        htmlPanelGrid.getChildren().add(strNombreEditInputMessage);
        
        HtmlOutputText strApellidoPaternoEditOutput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        strApellidoPaternoEditOutput.setId("strApellidoPaternoEditOutput");
        strApellidoPaternoEditOutput.setValue("Str Apellido Paterno: * ");
        htmlPanelGrid.getChildren().add(strApellidoPaternoEditOutput);
        
        InputTextarea strApellidoPaternoEditInput = (InputTextarea) application.createComponent(InputTextarea.COMPONENT_TYPE);
        strApellidoPaternoEditInput.setId("strApellidoPaternoEditInput");
        strApellidoPaternoEditInput.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{personalBean.personal.strApellidoPaterno}", String.class));
        LengthValidator strApellidoPaternoEditInputValidator = new LengthValidator();
        strApellidoPaternoEditInputValidator.setMaximum(50);
        strApellidoPaternoEditInput.addValidator(strApellidoPaternoEditInputValidator);
        strApellidoPaternoEditInput.setRequired(true);
        htmlPanelGrid.getChildren().add(strApellidoPaternoEditInput);
        
        Message strApellidoPaternoEditInputMessage = (Message) application.createComponent(Message.COMPONENT_TYPE);
        strApellidoPaternoEditInputMessage.setId("strApellidoPaternoEditInputMessage");
        strApellidoPaternoEditInputMessage.setFor("strApellidoPaternoEditInput");
        strApellidoPaternoEditInputMessage.setDisplay("icon");
        htmlPanelGrid.getChildren().add(strApellidoPaternoEditInputMessage);
        
        HtmlOutputText strApellidoMaternoEditOutput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        strApellidoMaternoEditOutput.setId("strApellidoMaternoEditOutput");
        strApellidoMaternoEditOutput.setValue("Str Apellido Materno: * ");
        htmlPanelGrid.getChildren().add(strApellidoMaternoEditOutput);
        
        InputTextarea strApellidoMaternoEditInput = (InputTextarea) application.createComponent(InputTextarea.COMPONENT_TYPE);
        strApellidoMaternoEditInput.setId("strApellidoMaternoEditInput");
        strApellidoMaternoEditInput.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{personalBean.personal.strApellidoMaterno}", String.class));
        LengthValidator strApellidoMaternoEditInputValidator = new LengthValidator();
        strApellidoMaternoEditInputValidator.setMaximum(50);
        strApellidoMaternoEditInput.addValidator(strApellidoMaternoEditInputValidator);
        strApellidoMaternoEditInput.setRequired(true);
        htmlPanelGrid.getChildren().add(strApellidoMaternoEditInput);
        
        Message strApellidoMaternoEditInputMessage = (Message) application.createComponent(Message.COMPONENT_TYPE);
        strApellidoMaternoEditInputMessage.setId("strApellidoMaternoEditInputMessage");
        strApellidoMaternoEditInputMessage.setFor("strApellidoMaternoEditInput");
        strApellidoMaternoEditInputMessage.setDisplay("icon");
        htmlPanelGrid.getChildren().add(strApellidoMaternoEditInputMessage);
        
        HtmlOutputText strUsuarioEditOutput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        strUsuarioEditOutput.setId("strUsuarioEditOutput");
        strUsuarioEditOutput.setValue("Str Usuario: * ");
        htmlPanelGrid.getChildren().add(strUsuarioEditOutput);
        
        InputTextarea strUsuarioEditInput = (InputTextarea) application.createComponent(InputTextarea.COMPONENT_TYPE);
        strUsuarioEditInput.setId("strUsuarioEditInput");
        strUsuarioEditInput.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{personalBean.personal.strUsuario}", String.class));
        LengthValidator strUsuarioEditInputValidator = new LengthValidator();
        strUsuarioEditInputValidator.setMaximum(45);
        strUsuarioEditInput.addValidator(strUsuarioEditInputValidator);
        strUsuarioEditInput.setRequired(true);
        htmlPanelGrid.getChildren().add(strUsuarioEditInput);
        
        Message strUsuarioEditInputMessage = (Message) application.createComponent(Message.COMPONENT_TYPE);
        strUsuarioEditInputMessage.setId("strUsuarioEditInputMessage");
        strUsuarioEditInputMessage.setFor("strUsuarioEditInput");
        strUsuarioEditInputMessage.setDisplay("icon");
        htmlPanelGrid.getChildren().add(strUsuarioEditInputMessage);
        
        HtmlOutputText strPasswordEditOutput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        strPasswordEditOutput.setId("strPasswordEditOutput");
        strPasswordEditOutput.setValue("Str Password: * ");
        htmlPanelGrid.getChildren().add(strPasswordEditOutput);
        
        InputTextarea strPasswordEditInput = (InputTextarea) application.createComponent(InputTextarea.COMPONENT_TYPE);
        strPasswordEditInput.setId("strPasswordEditInput");
        strPasswordEditInput.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{personalBean.personal.strPassword}", String.class));
        LengthValidator strPasswordEditInputValidator = new LengthValidator();
        strPasswordEditInputValidator.setMaximum(45);
        strPasswordEditInput.addValidator(strPasswordEditInputValidator);
        strPasswordEditInput.setRequired(true);
        htmlPanelGrid.getChildren().add(strPasswordEditInput);
        
        Message strPasswordEditInputMessage = (Message) application.createComponent(Message.COMPONENT_TYPE);
        strPasswordEditInputMessage.setId("strPasswordEditInputMessage");
        strPasswordEditInputMessage.setFor("strPasswordEditInput");
        strPasswordEditInputMessage.setDisplay("icon");
        htmlPanelGrid.getChildren().add(strPasswordEditInputMessage);
        
        HtmlOutputText chrEstadoEditOutput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        chrEstadoEditOutput.setId("chrEstadoEditOutput");
        chrEstadoEditOutput.setValue("Chr Estado:   ");
        htmlPanelGrid.getChildren().add(chrEstadoEditOutput);
        
        InputText chrEstadoEditInput = (InputText) application.createComponent(InputText.COMPONENT_TYPE);
        chrEstadoEditInput.setId("chrEstadoEditInput");
        chrEstadoEditInput.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{personalBean.personal.chrEstado}", Character.class));
        chrEstadoEditInput.setRequired(false);
        htmlPanelGrid.getChildren().add(chrEstadoEditInput);
        
        Message chrEstadoEditInputMessage = (Message) application.createComponent(Message.COMPONENT_TYPE);
        chrEstadoEditInputMessage.setId("chrEstadoEditInputMessage");
        chrEstadoEditInputMessage.setFor("chrEstadoEditInput");
        chrEstadoEditInputMessage.setDisplay("icon");
        htmlPanelGrid.getChildren().add(chrEstadoEditInputMessage);
        
        return htmlPanelGrid;
    }

	public HtmlPanelGrid populateViewPanel() {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        Application application = facesContext.getApplication();
        ExpressionFactory expressionFactory = application.getExpressionFactory();
        ELContext elContext = facesContext.getELContext();
        
        HtmlPanelGrid htmlPanelGrid = (HtmlPanelGrid) application.createComponent(HtmlPanelGrid.COMPONENT_TYPE);
        
        HtmlOutputText desarrolloProteccionsLabel = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        desarrolloProteccionsLabel.setId("desarrolloProteccionsLabel");
        desarrolloProteccionsLabel.setValue("Desarrollo Proteccions:   ");
        htmlPanelGrid.getChildren().add(desarrolloProteccionsLabel);
        
        HtmlOutputText desarrolloProteccionsValue = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        desarrolloProteccionsValue.setId("desarrolloProteccionsValue");
        desarrolloProteccionsValue.setValue("This relationship is managed from the DesarrolloProteccion side");
        htmlPanelGrid.getChildren().add(desarrolloProteccionsValue);
        
        HtmlOutputText informesLabel = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        informesLabel.setId("informesLabel");
        informesLabel.setValue("Informes:   ");
        htmlPanelGrid.getChildren().add(informesLabel);
        
        HtmlOutputText informesValue = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        informesValue.setId("informesValue");
        informesValue.setValue("This relationship is managed from the Informe side");
        htmlPanelGrid.getChildren().add(informesValue);
        
        HtmlOutputText intCargoIdLabel = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        intCargoIdLabel.setId("intCargoIdLabel");
        intCargoIdLabel.setValue("Int Cargo Id:   ");
        htmlPanelGrid.getChildren().add(intCargoIdLabel);
        
        HtmlOutputText intCargoIdValue = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        intCargoIdValue.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{personalBean.personal.intCargoId}", TablaMaestra.class));
        intCargoIdValue.setConverter(new TablaMaestraConverter());
        htmlPanelGrid.getChildren().add(intCargoIdValue);
        
        HtmlOutputText strNombreLabel = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        strNombreLabel.setId("strNombreLabel");
        strNombreLabel.setValue("Str Nombre:   ");
        htmlPanelGrid.getChildren().add(strNombreLabel);
        
        InputTextarea strNombreValue = (InputTextarea) application.createComponent(InputTextarea.COMPONENT_TYPE);
        strNombreValue.setId("strNombreValue");
        strNombreValue.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{personalBean.personal.strNombre}", String.class));
        strNombreValue.setReadonly(true);
        strNombreValue.setDisabled(true);
        htmlPanelGrid.getChildren().add(strNombreValue);
        
        HtmlOutputText strApellidoPaternoLabel = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        strApellidoPaternoLabel.setId("strApellidoPaternoLabel");
        strApellidoPaternoLabel.setValue("Str Apellido Paterno:   ");
        htmlPanelGrid.getChildren().add(strApellidoPaternoLabel);
        
        InputTextarea strApellidoPaternoValue = (InputTextarea) application.createComponent(InputTextarea.COMPONENT_TYPE);
        strApellidoPaternoValue.setId("strApellidoPaternoValue");
        strApellidoPaternoValue.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{personalBean.personal.strApellidoPaterno}", String.class));
        strApellidoPaternoValue.setReadonly(true);
        strApellidoPaternoValue.setDisabled(true);
        htmlPanelGrid.getChildren().add(strApellidoPaternoValue);
        
        HtmlOutputText strApellidoMaternoLabel = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        strApellidoMaternoLabel.setId("strApellidoMaternoLabel");
        strApellidoMaternoLabel.setValue("Str Apellido Materno:   ");
        htmlPanelGrid.getChildren().add(strApellidoMaternoLabel);
        
        InputTextarea strApellidoMaternoValue = (InputTextarea) application.createComponent(InputTextarea.COMPONENT_TYPE);
        strApellidoMaternoValue.setId("strApellidoMaternoValue");
        strApellidoMaternoValue.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{personalBean.personal.strApellidoMaterno}", String.class));
        strApellidoMaternoValue.setReadonly(true);
        strApellidoMaternoValue.setDisabled(true);
        htmlPanelGrid.getChildren().add(strApellidoMaternoValue);
        
        HtmlOutputText strUsuarioLabel = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        strUsuarioLabel.setId("strUsuarioLabel");
        strUsuarioLabel.setValue("Str Usuario:   ");
        htmlPanelGrid.getChildren().add(strUsuarioLabel);
        
        InputTextarea strUsuarioValue = (InputTextarea) application.createComponent(InputTextarea.COMPONENT_TYPE);
        strUsuarioValue.setId("strUsuarioValue");
        strUsuarioValue.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{personalBean.personal.strUsuario}", String.class));
        strUsuarioValue.setReadonly(true);
        strUsuarioValue.setDisabled(true);
        htmlPanelGrid.getChildren().add(strUsuarioValue);
        
        HtmlOutputText strPasswordLabel = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        strPasswordLabel.setId("strPasswordLabel");
        strPasswordLabel.setValue("Str Password:   ");
        htmlPanelGrid.getChildren().add(strPasswordLabel);
        
        InputTextarea strPasswordValue = (InputTextarea) application.createComponent(InputTextarea.COMPONENT_TYPE);
        strPasswordValue.setId("strPasswordValue");
        strPasswordValue.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{personalBean.personal.strPassword}", String.class));
        strPasswordValue.setReadonly(true);
        strPasswordValue.setDisabled(true);
        htmlPanelGrid.getChildren().add(strPasswordValue);
        
        HtmlOutputText chrEstadoLabel = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        chrEstadoLabel.setId("chrEstadoLabel");
        chrEstadoLabel.setValue("Chr Estado:   ");
        htmlPanelGrid.getChildren().add(chrEstadoLabel);
        
        HtmlOutputText chrEstadoValue = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        chrEstadoValue.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{personalBean.personal.chrEstado}", String.class));
        htmlPanelGrid.getChildren().add(chrEstadoValue);
        
        return htmlPanelGrid;
    }

	public Personal getPersonal() {
        if (personal == null) {
            personal = new Personal();
        }
        return personal;
    }

	public void setPersonal(Personal personal) {
        this.personal = personal;
    }

	public List<DesarrolloProteccion> getSelectedDesarrolloProteccions() {
        return selectedDesarrolloProteccions;
    }

	public void setSelectedDesarrolloProteccions(List<DesarrolloProteccion> selectedDesarrolloProteccions) {
        if (selectedDesarrolloProteccions != null) {
            personal.setDesarrolloProteccions(new HashSet<DesarrolloProteccion>(selectedDesarrolloProteccions));
        }
        this.selectedDesarrolloProteccions = selectedDesarrolloProteccions;
    }

	public List<Informe> getSelectedInformes() {
        return selectedInformes;
    }

	public void setSelectedInformes(List<Informe> selectedInformes) {
        if (selectedInformes != null) {
            personal.setInformes(new HashSet<Informe>(selectedInformes));
        }
        this.selectedInformes = selectedInformes;
    }

	public List<TablaMaestra> completeIntCargoId(String query) {
        List<TablaMaestra> suggestions = new ArrayList<TablaMaestra>();
        for (TablaMaestra tablaMaestra : tablaMaestraService.findAllTablaMaestras()) {
            String tablaMaestraStr = String.valueOf(tablaMaestra.getStrDescripcion() +  " "  + tablaMaestra.getChrEstado() +  " "  + tablaMaestra.getIntTablaId());
            if (tablaMaestraStr.toLowerCase().startsWith(query.toLowerCase())) {
                suggestions.add(tablaMaestra);
            }
        }
        return suggestions;
    }

	public String onEdit() {
        if (personal != null && personal.getDesarrolloProteccions() != null) {
            selectedDesarrolloProteccions = new ArrayList<DesarrolloProteccion>(personal.getDesarrolloProteccions());
        }
        if (personal != null && personal.getInformes() != null) {
            selectedInformes = new ArrayList<Informe>(personal.getInformes());
        }
        return null;
    }

	public boolean isCreateDialogVisible() {
        return createDialogVisible;
    }

	public void setCreateDialogVisible(boolean createDialogVisible) {
        this.createDialogVisible = createDialogVisible;
    }

	public String displayList() {
        createDialogVisible = false;
        findAllPersonals();
        return "personal";
    }

	public String displayCreateDialog() {
        personal = new Personal();
        createDialogVisible = true;
        return "personal";
    }

	public String persist() {
        String message = "";
        if (personal.getIntPersonalId() != null) {
            personalService.updatePersonal(personal);
            message = "Successfully updated";
        } else {
            personalService.savePersonal(personal);
            message = "Successfully created";
        }
        RequestContext context = RequestContext.getCurrentInstance();
        context.execute("createDialog.hide()");
        context.execute("editDialog.hide()");
        
        FacesMessage facesMessage = new FacesMessage(message);
        FacesContext.getCurrentInstance().addMessage(null, facesMessage);
        reset();
        return findAllPersonals();
    }

	public String delete() {
        personalService.deletePersonal(personal);
        FacesMessage facesMessage = new FacesMessage("Successfully deleted");
        FacesContext.getCurrentInstance().addMessage(null, facesMessage);
        reset();
        return findAllPersonals();
    }

	public void reset() {
        personal = null;
        selectedDesarrolloProteccions = null;
        selectedInformes = null;
        createDialogVisible = false;
    }

	public void handleDialogClose(CloseEvent event) {
        reset();
    }
}
